package com.cetc39.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cetc39.bean.Node;
import com.cetc39.dao.NodeDao;

public class NodeDaoImpl extends HibernateDaoSupport implements NodeDao {

	@Override
	public void save(Node n) {
		this.getHibernateTemplate().save(n);
	}

	@Override
	public void delete(int id) {
		this.getHibernateTemplate().delete(findById(id));
	}

	@Override
	public void update(Node n) {
		this.getHibernateTemplate().saveOrUpdate(n);
	}

	@Override
	public Node findById(int id) {
		Node n = this.getHibernateTemplate().get(Node.class, id);
		return n;
	}

	@Override
	public List<?> findAll() {
		List<?> nodeList = this.getHibernateTemplate().find("FROM Node");
		return nodeList;
	}

	@Override
	public List<?> findByFirstplanId(int id) {
		List<?> nodeList = this
				.getHibernateTemplate()
				.find("FROM Node WHERE firstplan.id=? AND updateornot=0 ORDER BY node_starttime",
						new Object[] { id });
		return nodeList;
	}

	@Override
	public void completeNode(int id, String flag) {
		Node n = this.getHibernateTemplate().get(Node.class, id);
		if ("complete".equals(flag)) {
			n.setStatus(2);
		} else if ("accept".equals(flag)) {
			n.setStatus(1);
		} else if ("reject".equals(flag)) {
			n.setStatus(0);
		}
		this.getHibernateTemplate().saveOrUpdate(n);
	}

	@Override
	public List<?> findByConditions(String[] conditions) throws ParseException {
		StringBuffer queryString = new StringBuffer();
		queryString.append("FROM Node as n WHERE 1=1 AND n.updateornot=0");
		if (conditions[0] != null && !"".equals(conditions[0])
				&& conditions[1] != null && !"".equals(conditions[1].trim())) {
			queryString
					.append(" AND n.node_endtime>=:startDate AND n.node_endtime<=:endDate");
		}
		if (conditions[2] != null && !"".equals(conditions[2].trim())) {
			queryString.append(" AND n.firstplan.project.head_dept.id="
					+ conditions[2]);
		}
		if (conditions[3] != null && !"".equals(conditions[3].trim())) {
			queryString.append(" AND n.firstplan.project.leader_39.id="
					+ conditions[3]);
		}
		if (conditions[4] != null && !"".equals(conditions[4].trim())) {
			queryString.append(" AND n.firstplan.project.project_type.id="
					+ conditions[4]);
		}
		if (conditions[5] != null && !"".equals(conditions[5].trim())) {
			queryString.append(" AND n.firstplan.project.project_manager.id="
					+ conditions[5]);
		}
		if (conditions[6] != null && !"".equals(conditions[6].trim())) {
			queryString.append(" AND n.firstplan.project.cpd_ps.id="
					+ conditions[6]);
		}
		if (conditions[7] != null && !"".equals(conditions[7].trim())) {
			queryString.append(" AND n.firstplan.project.project_status="
					+ conditions[7]);
		}
		if (conditions[8] != null && !"".equals(conditions[8].trim())) {
			queryString.append(" AND n.firstplan.project.project_code='"
					+ conditions[8] + "'");
		}
		Session session = this.getHibernateTemplate().getSessionFactory()
				.getCurrentSession();
		Query query = session.createQuery(queryString.toString());
		if (conditions[0] != null && !"".equals(conditions[0])
				&& conditions[1] != null && !"".equals(conditions[1])) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date startDate = sdf.parse(conditions[0]);
			Date endDate = sdf.parse(conditions[1]);
			query.setDate("startDate", startDate);
			query.setDate("endDate", endDate);
		}
		List<?> nodeList = query.list();

		return nodeList;

	}

	@Override
	public Node findByNodeId(String nodeId) {
		List<?> nodeList = this.getHibernateTemplate().find(
				"FROM Node WHERE node_id=" + nodeId);
		return (Node) nodeList.get(0);
	}

	@Override
	public List<?> findByFirstplanIdAndUpdateOrNot(int id) {
		List<?> nodeList = this
				.getHibernateTemplate()
				.find("FROM Node WHERE firstplan.id=? AND updateornot=0 ORDER BY node_starttime",
						new Object[] { id });
		return nodeList;
	}

	/*
	 * @Override public List<?> findByConditions(String[] conditions) {
	 * StringBuffer queryString = new StringBuffer("SELECT * FROM (");
	 * 
	 * StringBuffer nodeQueryString = new StringBuffer(
	 * "SELECT id, node_describe, node_endtime, node_result, `status`, firstplan_id FROM t_node WHERE 1=1"
	 * ); if(conditions[0] != null && !"".equals(conditions[0]) && conditions[1]
	 * != null && !"".equals(conditions[1])) {
	 * nodeQueryString.append("AND node_endtime BETWEEN " + conditions[0] +
	 * " AND " + conditions[1]); } StringBuffer projectQueryString = new
	 * StringBuffer(
	 * "SELECT plan_id, project_code, project_name FROM (SELECT id, project_code, project_name FROM t_projectinfo WHERE 1=1"
	 * ); if(conditions[2] != null && !"".equals(conditions[2])) {
	 * projectQueryString.append("AND head_dept_id=" + conditions[2]); }
	 * if(conditions[3] != null && !"".equals(conditions[3])) {
	 * projectQueryString.append("AND leader_39_id=" + conditions[3]); }
	 * if(conditions[4] != null && !"".equals(conditions[4])) {
	 * projectQueryString.append("AND project_type_id=" + conditions[4]); }
	 * if(conditions[5] != null && !"".equals(conditions[5])) {
	 * projectQueryString.append("AND project_manager_id=" + conditions[5]); }
	 * if(conditions[6] != null && !"".equals(conditions[6])) {
	 * projectQueryString.append("AND cpd_ps_id=" + conditions[6]); }
	 * if(conditions[7] != null && !"".equals(conditions[7])) {
	 * projectQueryString.append("AND project_status=" + conditions[7]); }
	 * if(conditions[8] != null && !"".equals(conditions[8])) {
	 * projectQueryString.append("AND project_code=" + conditions[8]); }
	 * projectQueryString.append(
	 * ") b LEFT JOIN (SELECT id AS plan_id, project_id FROM t_plan_one) c ON b.id = c.project_id"
	 * );
	 * 
	 * queryString.append(nodeQueryString);
	 * queryString.append(") a RIGHT JOIN (");
	 * queryString.append(projectQueryString);
	 * queryString.append(") d ON a.firstplan_id=d.plan_id");
	 * 
	 * String sql = queryString.toString(); System.out.println(sql); return
	 * null; }
	 */

	@Override
	public List<?> findAllNodesByFirstplanId(int id) {
		List<?> nodeList = this
				.getHibernateTemplate()
				.find("FROM Node WHERE firstplan.id=? ORDER BY node_starttime",
						new Object[] { id });
		return nodeList;
	}

}
